package com.zsl.shop_book.method;

import com.zsl.shop_book.annotation.RequestMapping;
import com.zsl.shop_book.annotation.Response;
import com.zsl.shop_book.annotation.阿里牛逼;
import com.zsl.shop_book.enums.RequestType;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.*;

public class HandlerMapping {
    private static Map<String,MVCMapping> urlMap=new HashMap<String,MVCMapping>();
    public static MVCMapping getUrlMap(String s){
        return urlMap.get(s);
    }
    private List<Class> list=new ArrayList<>();
    //加载扫描注解的包名称，即从什么包开始向下扫描
    public  void load(InputStream stream) {
        Properties properties=new Properties();
        try {
            properties.load(stream);
            String mvc=properties.getProperty("mvc");
            find(mvc);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private  void find(String mvc){
        String path = Thread.currentThread().getContextClassLoader().getResource(mvc).getPath();
        File file=new File(path);
        if(!file.exists()){
            throw  new RuntimeException();
        }
        //返回抽象路径名数组，这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录
        File[] dir=file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return file.isDirectory()||pathname.getName().endsWith("class");
            }
        });
        for(File f:dir){
            if(f.isDirectory()){
                find(f.getName());
            }else {
                //去掉.class
                String className=f.getName().substring(0,f.getName().length()-6);
                try {
                    //不需要初始化
                    list.add(Thread.currentThread().getContextClassLoader()
                            .loadClass(mvc.replace("/",".")+"."+className));
                    System.out.println(list);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        setUrl();
    }
    private void setUrl(){
        for (Class c:list){
            try {
                //如果有这个注解就获取方法上的注解
                if (c.getAnnotation(阿里牛逼.class)!=null){
                    Object object=c.getConstructor().newInstance();
                    Method[] methods=c.getMethods();
                    for (Method method:methods){
                        //RequestMapping返回视图，Response返回json对象字符串
                        if((method.getAnnotation(RequestMapping.class)!=null)&&(method.getAnnotation(Response.class)!=null)){
                            throw new RuntimeException("不能有多个请求");
                        }else {
                            if(method.getAnnotation(RequestMapping.class)!=null){
                                MVCMapping mvcMapping=new MVCMapping(object,method, RequestType.VIEW);
                                Object o=urlMap.put(asUrl(((阿里牛逼)c.getAnnotation(阿里牛逼.class)).value())
                                        +asUrl(method.getAnnotation(RequestMapping.class).value()),mvcMapping);
                                if(o!=null){
                                    throw new RuntimeException("请求地址重复");
                                }
                            }else if(method.getAnnotation(Response.class)!=null){
                                MVCMapping mvcMapping=new MVCMapping(object,method,RequestType.TEXT);
                                Object o=urlMap.put(asUrl(((阿里牛逼)c.getAnnotation(阿里牛逼.class)).value())
                                        +asUrl(method.getAnnotation(Response.class).value()),mvcMapping);
                                if(o!=null){
                                    throw new RuntimeException("请求地址重复");
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    private String asUrl(String s){
        return s.length()>0&&s.charAt(0)!='/'?'/'+s:s;
    }
}
